﻿using System;
using System.Collections.Generic;
using System.IO;

namespace DigitalQuran.Data
{
    /// <summary>
    /// Data Table to Hold Translations in Runtime
    /// </summary>
    public sealed class TranslationTable : Dictionary<string, string[]>
    {
        /// <summary>
        /// Get Dictionary of All Translations Loaded from Metadata File.
        /// <para>Key : Name of Translation</para>
        /// <para>Value: <see cref="DigitalQuran.Data.Translation"/></para>
        /// </summary>
        public Dictionary<string, Translation> AllTranslations { get; private set; }

        internal TranslationTable()
        {
            ReadMetaData();
        }

        /// <summary>
        /// Reads Translation Book from File if File exits on Local System.
        /// <para>System Will Search for File on <see cref="DigitalQuran.Client.Globals.TranslationDirectory"/> and <see cref="DigitalQuran.Client.Globals.TranslationDirectoryLocal"/></para>
        /// </summary>
        /// <param name="title">Title of Translation Book</param>
        public void LoadTranslationBook(string title)
        {
            Translation translation;
            if (AllTranslations.TryGetValue(title, out translation))
            {
                if (translation.IsDefault)
                {
                    using (var reader = new StreamReader(DigitalQuranDirectories.Translation + translation.Title + "." + Translation.FileType))
                    {
                        var lines = new string[Collection.VerseCollection.TotalVerses];

                        for (int i = 0; i < Collection.VerseCollection.TotalVerses; i++)
                        {
                            lines[i] = reader.ReadLine();
                        }

                        this.Add(title, lines);
                    }
                }
            }
        }

        /// <summary>
        /// Removes the Translation Book from Memory
        /// </summary>
        /// <param name="title">Title of Translation Book</param>
        public void UnloadTranslationBook(string title)
        {
            if (ContainsKey(title))
            {
                this.Remove(title);
            }
        }

        void ReadMetaData()
        {
            AllTranslations = new Dictionary<string, Translation>();

            using (var stream = new FileStream(DigitalQuranDirectories.Translation  + ".metadata", FileMode.Open))
            using (var reader = new StreamReader(stream))
            {
                reader.ReadLine(); // Ignore First Line                
                string[] parts;

                while (!reader.EndOfStream)
                {
                    parts = reader.ReadLine().Split('\t');

                    AllTranslations.Add(parts[0], new Translation
                    {
                        Title = parts[0],
                        Flag = parts[1],
                        Language = parts[2],
                        Translator = parts[3],
                    });
                }
            }
        }
    }

    /// <summary>
    /// Holds Translation Book Information in Runtime.
    /// <remarks>Translation Book Information will be loaded from metadata file</remarks>
    /// </summary>
    public struct Translation
    {
        /// <summary>
        /// Default File Extension for Translation Books
        /// </summary>
        public const string FileType = "txt";

        /// <summary>
        /// Flag Image File Name with Extension.
        /// <remarks>Flag Image Files will be located on <see cref="DigitalQuran.Client.Globals.TranslationDirectory"/>\Flags</remarks>
        /// </summary>
        public string Flag { get; set; }

        /// <summary>
        /// Language of Book
        /// </summary>
        public string Language { get; set; }

        /// <summary>
        /// Translator Name
        /// </summary>
        public string Translator { get; set; }

        /// <summary>
        /// Title of Translation Book
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// Returns true if Translation Book is in <see cref="DigitalQuran.Client.Globals.TranslationDirectory"/> Directory
        /// </summary>
        public bool IsDefault { get { return File.Exists(DigitalQuranDirectories.Translation + Title + "." + FileType); } }

        /// <summary>
        /// Returns true if Translation Book is in <see cref="DigitalQuran.Client.Globals.TranslationDirectoryLocal"/> Directory
        /// </summary>
        public bool OfflinePath { get { return File.Exists(DigitalQuranDirectories.TranslationLocal + Title + "." + FileType); } }
    }
}